import store from '@/store'
export function my2sj(data) {
  if (data == 0) {

    return "";
  }
  if (data < 60) {
    return data + "秒"
  }
  var m = myInt(data / 60);
  if (m < 60) {
    return m + "分" + (data % 60) + "秒"
  }
  var h = myInt(m / 60);
  if (h < 24) {
    return h + "小时" + (m % 60) + "分钟"
  }
  var d = myInt(h / 24);
  return d + "天" + (h % 24) + "小时";
}


export function formatJson(filterVal, tableData) {

  var res = [];
  for (var i = 0; i < tableData.length; i++) {
    var d = [];
    var s = tableData[i];
    for (var j = 0; j < filterVal.length; j++) {
      var key = filterVal[j]
      d.push(s[key]);
    }
    res.push(d);
  }

  return res;
}

export function formatJson2taxtArea(json) {
  var outStr = '',
    padIdx = 0,
    space = '     ';
  json = json.replace(/([\{\}\[\]])/g, '\r\n$1\r\n')
    .replace(/(\,)/g, '$1\r\n')
    .replace(/(\r\n\r\n)/g, '\r\n');
  (json.split('\r\n')).forEach(function (node, index) {
    var indent = 0,
      padding = '';
    if (node.match(/[\{\[]/)) {
      indent = 1;
    } else if (node.match(/[\}\]]/)) {
      padIdx = padIdx !== 0 ? --padIdx : padIdx;
    } else {
      indent = 0;
    }
    for (var i = 0; i < padIdx; i++) {
      padding += space;
    }
    outStr += padding + node + '\r\n';
    padIdx += indent;
  });
  return outStr;
}


export function getItemName(id) {


  var item = getItem(id);
  if (item) {

    return item.c + '-' + item.n + "-" + (item.g || "");
  } else {
    return id;
  }

}


export function getItem(id) {
  let items = store.state.dict.item
  for (var j = 0; j < items.length; j++) {
    if (items[j].i == id) {
      return items[j];
    }
  }
  return null;
}

export function dictsFormat(key, cellValue) {
  var dicts = store.state.dict.dicts
  let dict = dicts[key];
  if (!dict) {
    return cellValue
  }
  for (let i = 0; i < dict.length; i++) {
    if (dict[i].value == cellValue) {
      return dict[i].label;
    }
  }
  return cellValue
}

export function dictsSame(row, column) {

  let key = column.property;

  let val = row[key];
  return dictsFormat(key, val)
}

function myInt(d) {
  return parseInt(d);
}